package com.xywl.escort.view.farm;

import com.xywl.escort.bean.BaseReq;
import com.xywl.escort.bean.PreBuyReq;
import com.xywl.escort.bean.PreBuyResp;
import com.xywl.escort.bean.ProjectDetailResp;
import com.xywl.escort.net.BaseSubscriber;
import com.xywl.escort.net.ExceptionHandle;
import com.xywl.escort.net.api.service.ApiServer;
import com.xywl.escort.utils.RxUtil;

import javax.inject.Inject;

import timber.log.Timber;

/**
 * Created by 32422 on 2018/5/2.
 */

public class ProjectDetailPresenter implements ProjectDetailContract.Presenter {
    private ProjectDetailContract.View mView;
    private ApiServer mApiServer;

    @Inject
    ProjectDetailPresenter(ApiServer apiServer) {
        mApiServer = apiServer;
    }

    @Override
    public void subscribe() {

    }

    @Override
    public void takeView(ProjectDetailContract.View view) {
        this.mView = view;
    }

    @Override
    public void loadProjectDetail(String id) {
        BaseReq req = new BaseReq();
        req.id = id;
        mApiServer.findOneForFront(req)
                .compose(RxUtil.<ProjectDetailResp>applySchedulers(RxUtil.IO_ON_UI_TRANSFORMER_BACK_PRESSURE))
                .compose(mView.<ProjectDetailResp>bindToLife())
                .subscribe(new BaseSubscriber<ProjectDetailResp>() {
                    @Override
                    protected void hideDialog() {

                    }

                    @Override
                    protected void showDialog() {

                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseThrowable e) {
                        mView.loadProjectDetailFailure();
                    }

                    @Override
                    public void onNext(ProjectDetailResp resp) {
                        if (null != resp && null != resp.data && resp.isOk()) {
                            mView.loadProjectDetailSuccess(resp.data);
                        } else {
                            mView.loadProjectDetailFailure();
                        }
                    }
                });
    }

    @Override
    public void preBuy(String projectId, int isChooseAgreement, int num) {
        PreBuyReq req = new PreBuyReq();
        req.projectId = projectId;
        req.isChooseAgreement = isChooseAgreement;
        req.num = num;
        mApiServer.preBuy(req)
                .compose(RxUtil.<PreBuyResp>applySchedulers(RxUtil.IO_ON_UI_TRANSFORMER_BACK_PRESSURE))
                .compose(mView.<PreBuyResp>bindToLife())
                .subscribe(new BaseSubscriber<PreBuyResp>() {
                    @Override
                    protected void hideDialog() {

                    }

                    @Override
                    protected void showDialog() {

                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseThrowable e) {
                        mView.preBuyFailure("网络出现问题，请稍后重试");
                    }

                    @Override
                    public void onNext(PreBuyResp resp) {
                        if (null != resp) {
                            if (resp.isOk()) {
                                mView.preBuySuccess(resp);
                            } else {
                                mView.preBuyFailure(resp.msg);
                            }
                        } else {
                            mView.preBuyFailure("系统错误");
                        }
                    }
                });
    }

    @Override
    public void unSubscribe() {
        Timber.tag("ProjectDetailPresenter").e("unSubscribe");
        mView = null;
    }
}
